package cn.demo.mvc.controller;

import cn.demo.mvc.model.Book;
import cn.demo.mvc.service.BookService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/mvc/Book")
public class BookController extends HttpServlet {
    BookService bookService = new BookService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method == null || "list".equals(method)) {
            List<Book> books = bookService.listBooks();
            req.setAttribute("books", books);
            System.out.println(books.getClass().getName());
            req.getRequestDispatcher("/mvcPages/Book.jsp").forward(req, resp);
            return;
        }
        if ("search".equals(method)) {
            String name = req.getParameter("name");
            System.out.println(name);
            if ("".equals(name)){
                resp.sendRedirect(req.getContextPath()+"/mvc/Book");
                return;
            }
            List<Book> books = bookService.searchBook(name);
            req.setAttribute("books", books);
            req.getRequestDispatcher("/mvcPages/Book.jsp").forward(req, resp);
            return;
        }
        if ("add".equals(method)) {
            String name = req.getParameter("name");
            float price = Float.parseFloat(req.getParameter("price"));

            boolean flag = bookService.addBook(new Book(name, price));
            if (flag) {
                req.setAttribute("message", "添加信息成功");
            } else {
                req.setAttribute("message", "添加信息失败！！！！");
            }
            req.setAttribute("requestURI", req.getContextPath() + "/mvc/Book");
            req.getRequestDispatcher("/info.jsp").forward(req, resp);
            return;
        }
        if ("delete".equals(method)) {
            int id = Integer.parseInt(req.getParameter("id"));

            boolean flag = bookService.deleteBookById(id);
            if (flag) {
                req.setAttribute("message", "删除信息成功");
            } else {
                req.setAttribute("message", "删除信息失败！！！！");
            }
            req.setAttribute("requestURI", req.getContextPath() + "/mvc/Book");
            req.getRequestDispatcher("/info.jsp").forward(req, resp);
            return;
        }
        if ("update".equals(method)) {
            int id = Integer.parseInt(req.getParameter("id"));
            String name = req.getParameter("name");
            float price = Float.parseFloat(req.getParameter("price"));

            boolean flag = bookService.updateBook(new Book(id, name, price));
            if (flag) {
                req.setAttribute("message", "更新信息成功");
            } else {
                req.setAttribute("message", "更新信息失败！！！！");
            }
            req.setAttribute("requestURI", req.getContextPath() + "/mvc/Book");
            req.getRequestDispatcher("/info.jsp").forward(req, resp);
            return;
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
